﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using NIIT.SaoBacDau.Aupi.HospitalManagement.BusinessLogic;
using NIIT.SaoBacDau.Aupi.HospitalManagement.Entities;
using System.Configuration;
using System.Data.SqlClient;

namespace NIIT.SaoBacDau.Aupi.HospitalManagement
{
    public partial class Edit_Drug : Form
    {
        public Edit_Drug()
        {
            InitializeComponent();
        }

        E_DrugDetails ar = new E_DrugDetails();
        errordrugdetails err;

        private void Edit_Drug_Load(object sender, EventArgs e)
        {
            display();
        }

        private void display()
        {
            dataGridViewEdit.DataSource = BL_E_DrugDetails.RefressDrug();
        }

        private void dataGridViewEdit_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtdrugide.Text = dataGridViewEdit.CurrentRow.Cells[0].Value.ToString();
            txtdrugnamee.Text = dataGridViewEdit.CurrentRow.Cells[1].Value.ToString();
            txtunite.Text = dataGridViewEdit.CurrentRow.Cells[2].Value.ToString();
            txtpricee.Text = dataGridViewEdit.CurrentRow.Cells[3].Value.ToString();
            txtquantitye.Text = dataGridViewEdit.CurrentRow.Cells[4].Value.ToString();
            txtsourcee.Text = dataGridViewEdit.CurrentRow.Cells[5].Value.ToString();
            dateTimeprodutiongdatee.Value = Convert.ToDateTime(dataGridViewEdit.CurrentRow.Cells[6].Value);
            dateTimeusetimee.Value = Convert.ToDateTime(dataGridViewEdit.CurrentRow.Cells[7].Value);
            txtuseguidee.Text = dataGridViewEdit.CurrentRow.Cells[8].Value.ToString();
            dateTimeaddedwaree.Value = Convert.ToDateTime(dataGridViewEdit.CurrentRow.Cells[9].Value);
            comboBox1e.Text = dataGridViewEdit.CurrentRow.Cells[10].Value.ToString();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            ar.DrugID = Convert.ToInt32(txtdrugide.Text);
            ar.DrugTypeID = Convert.ToInt32(comboBox1e.SelectionLength.ToString());
            ar.NameDrug = txtdrugnamee.Text;
            ar.Unit = txtunite.Text;
            ar.Price = txtpricee.Text;
            ar.Source = txtsourcee.Text;
            ar.Quantity = txtquantitye.Text;
            ar.Productdate = Convert.ToDateTime(dateTimeprodutiongdatee.Text);
            ar.UseTime = Convert.ToDateTime(dateTimeusetimee.Text);
            ar.AddedWareHouseDate = Convert.ToDateTime(dateTimeaddedwaree.Text);
            ar.UseGuide = txtuseguidee.Text;
            err = BL_E_DrugDetails.Editdrug(ar);


            switch (err)
            {
                case errordrugdetails.NoError:
                    MessageBox.Show("Update successfully!");
                    display();
                    break;
                case errordrugdetails.ErrorEmpty:
                    break;
                case errordrugdetails.SqlError:
                    MessageBox.Show("Lỗi thực hiện chỉnh sửa chi tiết thuốc !", "Lỗi thực hiện", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    break;
                default:
                    break;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            ar.DrugID = Convert.ToInt32(txtdrugide.Text);
            ar.DrugTypeID = Convert.ToInt32(comboBox1e.SelectionLength.ToString());
            ar.NameDrug = txtdrugnamee.Text;
            ar.Unit = txtunite.Text;
            ar.Price = txtpricee.Text;
            ar.Source = txtsourcee.Text;
            ar.Quantity = txtquantitye.Text;
            ar.Productdate = Convert.ToDateTime(dateTimeprodutiongdatee.Text);
            ar.UseTime = Convert.ToDateTime(dateTimeusetimee.Text);
            ar.AddedWareHouseDate = Convert.ToDateTime(dateTimeaddedwaree.Text);
            ar.UseGuide = txtuseguidee.Text;
            err = BL_E_DrugDetails.Deletedrug(ar);

            validate();
            
            switch (err)
            {
                case errordrugdetails.NoError:

                    MessageBox.Show("delete successfully!");
                    display();
                    break;
                case errordrugdetails.ErrorEmpty:
                  errorProvider1.SetError(txtdrugide, "delete false");
                    break;
                default:
                    break;
            }
        }

        private bool validate()
        { 
        bool sta = true;
        if (txtdrugide.Text == "")
        {
            errorProvider1.SetError(txtdrugide, "loi");
            sta = false;
        }
        else
        { }
        return sta;

        }

        private void comboBox1e_Click(object sender, EventArgs e)
        {
            comboBox1e.DataSource = BL_E_DugType.DrugType();
            comboBox1e.ValueMember = "DrugTypeID";
            comboBox1e.DisplayMember = "DrugTypeName";
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            display();
        }

        private void  btnSearch_Click(object sender, EventArgs e)
        {
            
            ar.NameDrug = txt_SearchName.Text;
            if (txt_SearchName.Text == "")
            {
                errorProvider1.SetError(txt_SearchName, "xin dien tu khoa");
                display();
            }
            else
            {
                dis(ar);
            }
        }

        private void dis(E_DrugDetails E_DrugDetails)
        {
            dataGridViewEdit.DataSource = BL_E_DrugDetails.ListSearch(E_DrugDetails);
        }
    }
}
